//
//  growthLeftTVController.m
//  SKaURobot
//
//  Created by cdong on 16/3/1.
//  Copyright © 2016年 cdong. All rights reserved.
//

#import "growthLeftTVController.h"
#import "growthLeftTViCell.h"
#import "AFHTTPSessionManager.h"
#import "growthPhotoModel.h"
#import "UIImageView+WebCache.h"
#import "growthDetailVController.h"

@interface growthLeftTVController ()

@property (nonatomic ,strong)NSMutableArray *growthPhotoMArr;

@end

@implementation growthLeftTVController

- (NSMutableArray *)growthPhotoMArr{
    if (_growthPhotoMArr == nil) {
        _growthPhotoMArr = [[NSMutableArray alloc]init];
    }
    return _growthPhotoMArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[growthLeftTViCell class] forCellReuseIdentifier:@"growthLeftTViCell"];
    [self handleData];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (void)handleData{
    NSString *urlStr = [NSString stringWithFormat:@"http://api.au-smart.cn/index.php?act=picList&sn=012016012900000100017&uid=30&sesskey=62da4a20ffe542487f183f7a0de5b6d3&page=1"];
    NSString *utf = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:utf parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
    
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        NSNumber *error = [responseObject objectForKey:@"error"];
        if (error.integerValue == 0) {
            for (NSDictionary *dict in responseObject[@"list"]) {
                growthPhotoModel *photosM = [[growthPhotoModel alloc]init];
                [photosM setValuesForKeysWithDictionary:dict];
                [self.growthPhotoMArr addObject:photosM];
            }
            [self.tableView reloadData];
        }else{
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 214.0f;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 13;//self.growthPhotoMArr.count;
}

#pragma update
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    growthLeftTViCell *cell = [[growthLeftTViCell alloc]init];
    cell.selected = NO;
//    growthLeftTViCell *cell = [tableView dequeueReusableCellWithIdentifier:@"growthLeftTViCell"];
//    if (cell == nil) {
//        cell = [[growthLeftTViCell alloc]init];
//    }
    if (indexPath.row%3 == 0) {
        cell.imageV1.image = [UIImage imageNamed:@"point_red"];
    }else if (indexPath.row%3 == 1){
        cell.imageV1.image = [UIImage imageNamed:@"point_orange"];
    }else{
        cell.imageV1.image = [UIImage imageNamed:@"point_blue"];
    }
    cell.photoV.image = [UIImage imageNamed:@"default_growth"];
    cell.timeL.text = @"sdddfd";
//    growthPhotoModel *growthM = self.growthPhotoMArr[indexPath.row];
//    [cell.photoV sd_setImageWithURL:[NSURL URLWithString:growthM.src] placeholderImage:[UIImage imageNamed:@"default_growth"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    
//    }];
    
//    if (indexPath.row == 0) {
//        NSString *first = [growthM.addtime substringToIndex:6];
//        NSString *last = [growthM.addtime substringFromIndex:6];
//        cell.timeL.text = [NSString stringWithFormat:@"今天（%@）%@",first,last];
//    }else{
//        cell.timeL.text = growthM.addtime;
//    }
    
    return cell;
}

// 详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    growthDetailVController *detailVC = [[growthDetailVController alloc]init];
    [self.navigationController pushViewController:detailVC animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
